<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>


    <div id="content">
        true
    </div>

    <button onclick="change()">
    发布事件
  </button>
    <script>
        class EventBus {

            _events = {};

            on(event, callback) {
                (this._events[event] || (this._events[event] = [])).push(callback);
                console.log(this._events)
            }


            emit(event) {
                console.log(this._events);
                let cbs = [...this._events[event]];
                if (cbs) {
                    for (let i = 0; i < cbs.length; i++) {
                        try {
                            cbs[i].apply(null, [...arguments].slice(1));
                        } catch (e) {
                            new Error(e, `event handler for "${event}"`)
                        }
                    }
                }
            }

        }


        const bus = new EventBus();
        let test = true;
        bus.on('changeContent', function(e) {
            test = !test;
            document.getElementById("content").innerText = test;
        });

        function change() {
            console.log("有点问题")
            bus.emit('changeContent', "你好呀");
        }
    </script>

</body>

</html>